package com.catl.plc.master;

import com.catl.plc.handler.ModbusEntity;
import com.catl.plc.handler.PlcHandler;
import com.catl.plc.meta.RegisterTypeEnums;
import com.serotonin.modbus4j.ModbusMaster;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description TODO
 * @ClassName abstractMaster
 * @Date 2022/4/12 9:28
 * @Author 王东平
 */
public abstract class AbstractPlcMaster implements PlcOperation {
    @Getter
    @Setter
    private Plc4jMaster plc4jMaster;
    @Getter
    @Setter
    private ModbusMaster modbus4jMaster;
    @Getter
    @Setter
    private Consumer errorHandler=error->{};
    @Getter
    @Setter
    private Consumer okHandler=ok->{};
    /**
     * 1:选用modbus4jMaster ; 2 : 选用 plc4jMaster
     */
    @Getter
    @Setter
    private int type=2;
    @Getter
    @Setter
    private int slaveId = 1 ;
    @Getter
    @Setter
    private List<Integer> slaveIds = Stream.of(Integer.parseInt("1")).collect(Collectors.toList());

    @Override
    public boolean writeRegisters(RegisterTypeEnums registerType, int slaveId, int startOffset, List<Integer> commands){
        return PlcHandler.writeRegisters(this,registerType,slaveId,startOffset,commands);
    }
    @Override
    public boolean writeRegister(RegisterTypeEnums registerType, int slaveId, int startOffset, Integer command){
        return PlcHandler.writeRegister(this,registerType,slaveId,startOffset,command);
    }
    @Override
    public boolean writeRegisters(RegisterTypeEnums registerType, int startOffset, List<Integer> commands){
        return PlcHandler.writeRegisters(this, registerType,  startOffset,  commands);
    }
    @Override
    public boolean writeRegister(RegisterTypeEnums registerType, int startOffset, Integer command){
        return PlcHandler.writeRegister(this,registerType,startOffset,command);
    }
    @Override
    public boolean writeEntity(int slaveId, ModbusEntity entity){
        return PlcHandler.writeEntity(this, slaveId,  entity);
    }
    @Override
    public boolean writeEntity(ModbusEntity entity){
        return PlcHandler.writeEntity(this, entity);
    }
    @Override
    public boolean writeEntities(int slaveId, List<? extends ModbusEntity> entities){
        return PlcHandler.writeEntities(this, slaveId,  entities);
    }
    @Override
    public boolean writeEntities(List<? extends ModbusEntity> entities){
        return PlcHandler.writeEntities(this, entities);
    }

    @Override
    public List<Integer> readRegister(RegisterTypeEnums registerType, int slaveId, int startOffset, int length){
        return PlcHandler.readRegister(this, registerType, slaveId, startOffset, length);
    }
    @Override
    public List<Integer> readRegister(RegisterTypeEnums registerType, int startOffset, int length){
        return PlcHandler.readRegister(this, registerType, startOffset, length);
    }
    @Override
    public List<Integer> readByEntity(int slaveId, Class<? extends ModbusEntity> clazz){
        return PlcHandler.readByEntity(this, slaveId,  clazz);
    }
    @Override
    public List<Integer> readByEntity(Class<? extends ModbusEntity> clazz){
        return PlcHandler.readByEntity( this,clazz);
    }
    @Override
    public List<Integer> getShortData(int slaveId, Class<? extends ModbusEntity> clazz){
        return PlcHandler.getShortData(this, slaveId,  clazz);
    }
    @Override
    public List<Integer> getShortData(Class<? extends ModbusEntity> clazz){
        return PlcHandler.getShortData(this, clazz);
    }
    @Override
    public List<char[]> getCharData(int slaveId, Class<? extends ModbusEntity> clazz){
        return PlcHandler.getCharData(this, slaveId,  clazz);
    }
    @Override
    public List<char[]> getCharData(Class<? extends ModbusEntity> clazz){
        return PlcHandler.getCharData(this, clazz);
    }
    @Override
    public ModbusEntity readEntity(int slaveId, Class<? extends ModbusEntity> clazz){
        return PlcHandler.readEntity( this,slaveId,  clazz);
    }
    @Override
    public ModbusEntity readEntity(Class<? extends ModbusEntity> clazz){
        return PlcHandler.readEntity(this, clazz);
    }
    @Override
    public List<? extends ModbusEntity> readEntities(int slaveId, Class<? extends ModbusEntity> clazz){
        return PlcHandler.readEntities(this,  slaveId,  clazz);
    }
    @Override
    public List<? extends ModbusEntity> readEntities(Class<? extends ModbusEntity> clazz){
        return PlcHandler.readEntities(this, clazz);
    }

    /**
     * 后置处理器
     */
    public void postProcessor(){

    }

}
